package com.lm.mapper;

import com.lm.pojo.Message;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@org.apache.ibatis.annotations.Mapper
public interface MessageMapper extends Mapper<Message> {
    
    @Select("SELECT * FROM ylb_msg_store WHERE (sender_uid = #{user1Uid} AND receiver_uid = #{user2Uid}) " +
            "OR (sender_uid = #{user2Uid} AND receiver_uid = #{user1Uid}) ORDER BY msg_time ASC")
    List<Message> getChatHistory(@Param("user1Uid") String user1Uid, @Param("user2Uid") String user2Uid);

    @Select("SELECT * FROM ylb_msg_store WHERE receiver_uid = #{uid} AND msg_isread = 0 ORDER BY msg_time DESC")
    List<Message> getUnreadMessages(@Param("uid") String uid);

    @Update("UPDATE ylb_msg_store SET msg_isread = 1 WHERE id = #{messageId}")
    void markAsRead(@Param("messageId") Integer messageId);

    @Select("SELECT DISTINCT CASE " +
            "WHEN sender_uid = #{uid} THEN receiver_uid " +
            "ELSE sender_uid END as chat_user " +
            "FROM ylb_msg_store " +
            "WHERE sender_uid = #{uid} OR receiver_uid = #{uid} " +
            "ORDER BY msg_time DESC")
    List<String> getChatUsers(@Param("uid") String uid);

    @Select("SELECT COUNT(*) FROM ylb_msg_store " +
            "WHERE sender_uid = #{otherUid} AND receiver_uid = #{uid} AND msg_isread = 0")
    int getUnreadCount(@Param("uid") String uid, @Param("otherUid") String otherUid);
} 